package com.biallen.datastructure.linkedlist;

import com.biallen.datastructure.entity.HeroNode;

public class SingleLinkedList {


    /**
     * 初始化头节点
     */
    private HeroNode headNode = new HeroNode(0, "", "");

    /**
     * 添加节点
     *
     * @param node
     */
    public void add(HeroNode node) {
        HeroNode temp = headNode;
//        while (true) {
//            if (temp.next == null) {
//                break;
//            }
//            temp = temp.next;
//        }
//        temp.next = node;
        while (temp != null) {
            if (temp.next == null) {
                temp.next = node;
                break;
            }
            temp = temp.next;
        }
    }

    /**
     * 顺序添加节点
     *
     * @param node
     */
    public void addByOrder(HeroNode node) {

        HeroNode temp = headNode;
        boolean success = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == node.no) {
                success = true;
                break;
            }
            if (temp.next.no > node.no) {

                break;
            }
            temp = temp.next;

        }
        if (success) {
            System.out.println("编号已存在:" + node.no);
        } else {
            HeroNode tm = temp.next;
            temp.next = node;
            node.next = tm;
        }

    }


    /**
     * 获取长度 ，优化 在添加节点时 设置全局变量 累加节点 即可获取链表的长度
     *
     * @return
     */
    public int getSize() {

        HeroNode tempNode = headNode.next;
        if (headNode.next == null) {
            return 0;
        }
        Integer size = 0;
        while (tempNode != null) {
            size++;
            tempNode = tempNode.next;
        }
        return size;
    }

    /**
     * 链表反转
     * @param node
     * @return
     */
    public  HeroNode reversalList(HeroNode node){
           if(node ==null || node.next ==null){
               return  node;
           }
           //当前节点
            HeroNode cur = node;
           // HeroNode pre =new HeroNode(0,"","");
            HeroNode pre =null;//上一个节点
           while (cur!=null){
               // HeroNode cur = tempNode;
                //缓存下一个节点
                HeroNode next =  cur.next;
               //当前节点的下一个节点是上一个节点
                cur.next=pre;
               //当前节点赋给上一个节点
                pre=cur;
                //将缓存的下一个节点赋给当前节点
                cur=next;
           }
        //newNode.next=pre;
        return pre;
    }

    public  HeroNode getHeadNode(){
        return  headNode;
    }

    /**
     * 根据节点的索引值查找节点
     * @param index
     * @return
     */
    public  HeroNode findNodeByIndex(int index){
        if(headNode.next==null){
            return  null;
        }

        HeroNode node = headNode.next;
        int indx =0;
        while (node!=null){

            if(indx == index){
                break;
            }
            indx ++;
            node = node.next;
        }
        return  node;
    }
    /**
     * 更新
     *
     * @param node
     * @return
     */
    public boolean update(HeroNode node) {
        HeroNode tempNode = this.headNode;
        boolean success = false;
        while (true) {
            if (tempNode.next == null) {
                break;
            }
            if (tempNode.next.no == node.no) {
                success = true;
                break;
            }
            tempNode = tempNode.next;
        }
        if (success) {
            tempNode.next.name = node.name;
            tempNode.next.nickName = node.nickName;
            return true;
        }
        return false;
    }

    /**
     * 删除节点
     */
    public boolean del(HeroNode node) {

        HeroNode tempNode = headNode;
        boolean success = false;
        while (true) {
            if (tempNode.next == null) {
                break;
            }
            if (tempNode.next.no == node.no) {
                success = true;
                break;
            }
            tempNode = tempNode.next;
        }
        if (success) {
            HeroNode next = tempNode.next.next;
            tempNode.next = next;
            return true;
        }
        return false;

    }

    /**
     * 展示节点
     */
    public void displayList() {
        HeroNode temp = headNode.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
        System.out.println("展示完毕--------------->");
//        HeroNode temp = headNode.next;
//        while (true) {
//            //判断是否到链表最后
//            if (temp == null) {
//                break;
//            }
//            //输出节点的信息
//            System.out.println(temp);
//            //将 temp 后移， 一定小心
//            temp = temp.next;
//        }
    }

    public void displayList(HeroNode node) {
        HeroNode temp = node;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
        System.out.println("展示完毕--------------->");

    }
}
